<template>
  <div class="app-container">
    <div class="cardBox">
      <!-- <el-button
        @click="gobackWay"
        type="text"
        icon="el-icon-arrow-left"
        style="color: rgba(0, 0, 0, 0.85)"
        >返回</el-button
      > -->
      <!-- <el-divider direction="vertical"></el-divider> -->
      <span>等级默认配置项</span>
    </div>
    <div v-loading="loading" class="cardBox marginTop">
      <el-form
        style="width: 30vw"
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="120px"
        class="demo-ruleForm"
      >
        <el-form-item label="启用会员等级" prop="freeze">
          <el-radio v-model="ruleForm.is_open_level" label="1">开启</el-radio>
          <el-radio v-model="ruleForm.is_open_level" label="0">关闭</el-radio>
          <div class="tips">用户会员等级是否启用</div>
        </el-form-item>
        <el-form-item label="订单赠送经验" prop="order_gift_experience">
          <el-input
            type="text"
            onkeyup="value=value.replace(/\D/g,'')"
            v-model="ruleForm.order_gift_experience"
            autocomplete="off"
          ></el-input>
          <div class="tips">
            下单赠送会员经验比例（实际支付1元赠送多少经验）
          </div>
        </el-form-item>
        <el-form-item label="邀请赠送经验" prop="invite_gift_experience">
          <el-input
            type="text"
            onkeyup="value=value.replace(/\D/g,'')"
            v-model="ruleForm.invite_gift_experience"
            autocomplete="off"
          ></el-input>
          <div class="tips">邀请一个新用户赠送经验值</div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="gradeSubmitForm('ruleForm')"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { getLevelConfig, saveLevelConfig } from "@/api/user/index";
export default {
  data() {
    return {
      loading: false,
      ruleForm: {
        is_open_level: "",
        order_gift_experience: "",
        invite_gift_experience: "",
      },
      rules: {
        is_open_level: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (value == "") {
                return callback(new Error("请选择是否启用"));
              }
              callback();
            },
            trigger: "change",
          },
        ],
        order_gift_experience: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (!value) {
                return callback(new Error("订单赠送经验不能为空"));
              }
              callback();
            },
            trigger: "blur",
          },
        ],
        invite_gift_experience: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (!value) {
                return callback(new Error("邀新赠送经验不能为空"));
              }
              callback();
            },
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getLevelConfigWay();
  },
  methods: {
    getLevelConfigWay() {
      this.loading = true;
      getLevelConfig()
        .then((res) => {
          this.loading = false;
          if (res.code !== 200) {
            this.$messages.error(res.msg);
            return;
          }
          this.loading = false;
          this.ruleForm = res.data;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    gradeSubmitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          saveLevelConfig(this.ruleForm)
            .then((res) => {
              if (res.code !== 200) {
                this.$messages.error(res.msg);
                return;
              }
              this.$messages.success("已保存");
              this.getLevelConfigWay();
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.tips {
  margin-top: 0px;
  color: #c0c4cc;
  font-size: 12px;
  line-height: 26px;
}
</style>